package com.example.hehecat.yuejian1.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ActivityReplyContract;
import com.example.hehecat.yuejian1.mvp.model.ActivityReplyModel;
import com.example.hehecat.yuejian1.mvp.model.bean.ReplyBean;
import com.example.hehecat.yuejian1.mvp.model.bean.SuccessBean;
import com.example.hehecat.yuejian1.mvp.presenter.ActivityReplyPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ActivityReplayAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportActivity;
import rx.Subscriber;


public class ReplyActivityActivity extends SupportActivity implements ActivityReplyContract.View {
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.btn_send)
    Button btnSend;
    int activity_id;
    int user_id;
    @BindView(R.id.comment_text)
    EditText commentText;
    @BindView(R.id.foodinfo_return)
    ImageView activityReplyReturn;
    private ActivityReplayAdapter adapter;
    private ActivityReplyPresenter presenter;

    @OnClick(R.id.foodinfo_return)
    void activityReturn() {
        finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reply);
        ButterKnife.bind(this);

        //获得参数
        Intent intent = getIntent();
        activity_id = intent.getIntExtra("activity_id", 2);
        user_id = MyStaticVariable.newInstance().getUserid();
        Log.d("reply", "onCreate: activity_id"+activity_id + " userid"+user_id);
        //创建布局管理
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvContent.setLayoutManager(layoutManager);
        rvContent.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ActivityReplayAdapter(R.layout.reply_item);
        rvContent.setAdapter(adapter);

        //给RecyclerView设置适配器
        presenter = new ActivityReplyPresenter(this);
        presenter.updateAdapterData(activity_id);
        rvContent.setAdapter(adapter);

    }

    @Override
    public void updateAdapterData(List<ReplyBean> data) {
        adapter.addData(data);
    }

    @OnClick(R.id.btn_send)
    void reply() {
        ActivityReplyModel model = new ActivityReplyModel();
        String comment = commentText.getText().toString();
        if (comment.equals("")) {
            Toast.makeText(this, "内容不能为空！", Toast.LENGTH_SHORT).show();
        }else {
            model.replyActivity(activity_id, user_id, comment, new Subscriber<SuccessBean>() {
                @Override
                public void onCompleted() {
                    Log.d("reply", "completed: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.d("reply", "onError: " + e.getMessage());
                }

                @Override
                public void onNext(SuccessBean replyBeans) {
                    if (replyBeans.getSuccess() == 1) {
                        ReplyBean replyBean = new ReplyBean();
                        replyBean.setActivityid(activity_id);
                        replyBean.setComment(comment);
                        replyBean.setUserid(user_id);
                        commentText.setText("");
                        adapter.addData(replyBean);
                        adapter.notifyDataSetChanged();
                    } else {
                        Toast.makeText(ReplyActivityActivity.this, "评论失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }


}
